package com.ly10111.customedittext.ime.dict;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;

public class ZyDict implements IDict {

    public SQLiteDatabase db = null;

    public static void main(String[] args) {
        //1630421229
        //16,30,42,12,29,40
        //ㄋㄧˇㄇㄣ˙
        char c1 = 12555;
        char c2 = 12583;
        char c3 = 711;
        char c4 = 12551;
        char c5 = 12579;
        char c6 = 729;
        System.out.print(c1);
        System.out.print(c2);
        System.out.print(c3);
        System.out.print(c4);
        System.out.print(c5);
        System.out.print(c6);
    }

    @Override
    public HashMap<String, String> getDictCodeMap() {
        HashMap<String, String> codeMap = new HashMap<>();
        codeMap.put("12549", "10");
        codeMap.put("12550", "11");
        codeMap.put("12551", "12");
        codeMap.put("12552", "13");
        codeMap.put("12553", "14");
        codeMap.put("12554", "15");
        codeMap.put("12555", "16");
        codeMap.put("12556", "17");
        codeMap.put("12557", "18");
        codeMap.put("12558", "19");
        codeMap.put("12559", "1A");
        codeMap.put("12560", "1B");
        codeMap.put("12561", "1C");
        codeMap.put("12562", "1D");
        codeMap.put("12563", "1E");
        codeMap.put("12564", "1F");
        codeMap.put("12565", "1G");
        codeMap.put("12566", "1H");
        codeMap.put("12567", "1I");
        codeMap.put("12568", "1J");
        codeMap.put("12569", "1K");
        codeMap.put("12570", "20");
        codeMap.put("12571", "21");
        codeMap.put("12572", "22");
        codeMap.put("12573", "23");
        codeMap.put("12574", "24");
        codeMap.put("12575", "25");
        codeMap.put("12576", "26");
        codeMap.put("12577", "27");
        codeMap.put("12578", "28");
        codeMap.put("12579", "29");
        codeMap.put("12580", "2A");
        codeMap.put("12581", "2B");
        codeMap.put("12582", "2C");
        codeMap.put("12583", "30");
        codeMap.put("12584", "31");
        codeMap.put("12585", "32");
        codeMap.put("729", "40");
        codeMap.put("714", "41");
        codeMap.put("711", "42");
        codeMap.put("715", "43");
        return codeMap;
    }

    @Override
    public void open(Context context) {
        db = SQLiteDatabase.openDatabase(String.format("/data/data/%s/databases/dict.db0", context.getPackageName()), null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public void close() {
        db.close();
    }

    @Override
    public ArrayList<DictBean> cursor(String text, HashMap<String, String> codeMap, boolean isWord, boolean isPhrases) {
        if (isPhrases && isWord) text = text.substring(text.length() - 1);
        ArrayList<DictBean> dList = new ArrayList<>();
        Cursor cursor;
        if (isPhrases) {
            if (isWord)
                cursor = db.rawQuery("select id,word from phrases where word like ? and length(word)>1 group by word order by use desc limit 50", new String[]{text + "%"});
            else
                cursor = db.rawQuery("select id,word from phrases where zyCode like ? and length(word)>1 group by word order by use desc limit 50", new String[]{text + "%"});
        } else {
            String code = "";
            for (int i = 0; i < text.length(); i++)
                code += codeMap.get((int) text.charAt(i) + "");
            cursor = db.rawQuery("select id,word from words where zyCode like ? group by word order by use desc limit 50", new String[]{code + "%"});
            if (!cursor.moveToNext()) {
                cursor.close();
                return cursor(code, codeMap, false, true);
            } else cursor.moveToPrevious();
        }
        while (cursor.moveToNext()) {
            long id = cursor.getLong(0);
            String word = cursor.getString(1);
            if (isPhrases && isWord) word = word.substring(1, word.length());
            DictBean dictBean = new DictBean(id, word);
            dList.add(dictBean);
        }
        cursor.close();
        return dList;
    }

    @Override
    public void addUse(DictBean dictBean) {
        if (dictBean.getWord().length() == 1)
            db.execSQL("update words set use=use+1 where id=?", new String[]{dictBean.getId() + ""});
        else
            db.execSQL("update phrases set use=use+1 where id=?", new String[]{dictBean.getId() + ""});
    }
}
